<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 手机商城</title>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #2c3e50;
        }

        /* 登录容器 */
        .login-container {
            background: white;
            border-radius: 12px;
            padding: 3rem;
            width: 100%;
            max-width: 400px;
            margin: 2rem;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        }

        .logo-section {
            text-align: center;
            margin-bottom: 2rem;
        }

        .logo {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
        }

        .title {
            font-size: 1.5rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 0.5rem;
        }

        .subtitle {
            color: #7f8c8d;
            font-size: 0.9rem;
        }

        /* 表单样式 */
        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            color: #2c3e50;
            font-weight: 500;
        }

        .form-input {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s;
            background: #f8fafc;
        }

        .form-input:focus {
            outline: none;
            border-color: #3498db;
            background: white;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        }

        /* 登录类型选择 */
        .user-type-selector {
            display: flex;
            background: #f8f9fa;
            border-radius: 8px;
            padding: 4px;
            margin-bottom: 1.5rem;
        }

        .type-option {
            flex: 1;
            padding: 0.75rem;
            text-align: center;
            border: none;
            background: transparent;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s;
            color: #7f8c8d;
        }

        .type-option.active {
            background: #3498db;
            color: white;
            box-shadow: 0 2px 8px rgba(52, 152, 219, 0.3);
        }

        /* 按钮样式 */
        .login-btn {
            width: 100%;
            background: #3498db;
            color: white;
            border: none;
            padding: 0.75rem;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 1rem;
        }

        .login-btn:hover {
            background: #2980b9;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
        }

        .login-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
            transform: none;
        }

        /* 链接样式 */
        .auth-links {
            text-align: center;
            margin-top: 1.5rem;
        }

        .auth-link {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
            margin: 0 1rem;
            transition: color 0.3s;
        }

        .auth-link:hover {
            color: #2980b9;
        }

        /* 消息样式 */
        .message {
            padding: 0.75rem;
            border-radius: 6px;
            margin-bottom: 1rem;
            font-size: 0.9rem;
            text-align: center;
            display: none;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        /* 返回首页链接 */
        .back-home {
            position: absolute;
            top: 2rem;
            left: 2rem;
            color: white;
            text-decoration: none;
            font-weight: 500;
            opacity: 0.9;
            transition: opacity 0.3s;
        }

        .back-home:hover {
            opacity: 1;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .login-container {
                padding: 2rem;
                margin: 1rem;
            }

            .back-home {
                top: 1rem;
                left: 1rem;
            }
        }
    </style>
</head>

<body>
    <a href="index.html" class="back-home">← 返回首页</a>
    
    <div class="login-container">
        <div class="logo-section">
            <div class="logo">📱</div>
            <h1 class="title">欢迎回来</h1>
            <p class="subtitle">登录您的账户</p>
        </div>

        <div id="message" class="message"></div>

        <form id="loginForm">
            <div class="user-type-selector">
                <button type="button" class="type-option active" data-type="user">普通用户</button>
                <button type="button" class="type-option" data-type="admin">管理员</button>
            </div>

            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input type="text" class="form-input" id="username" name="username" required>
            </div>

            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <input type="password" class="form-input" id="password" name="password" required>
            </div>

            <button type="submit" class="login-btn">登录</button>
        </form>

        <div class="auth-links">
            <a href="register.html" class="auth-link">还没有账户？立即注册</a>
        </div>
    </div>

    <script>
        let currentUserType = 'user';

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            initializeTypeSelector();
            initializeForm();
        });

        // 初始化用户类型选择器
        function initializeTypeSelector() {
            const typeOptions = document.querySelectorAll('.type-option');
            
            typeOptions.forEach(option => {
                option.addEventListener('click', function() {
                    // 移除所有active类
                    typeOptions.forEach(opt => opt.classList.remove('active'));
                    // 添加active类到当前选项
                    this.classList.add('active');
                    // 更新当前用户类型
                    currentUserType = this.dataset.type;
                });
            });
        }

        // 初始化表单
        function initializeForm() {
            const form = document.getElementById('loginForm');
            
            form.addEventListener('submit', async function(e) {
                e.preventDefault();
                await handleLogin();
            });
        }

        // 处理登录
        async function handleLogin() {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();

            if (!username || !password) {
                showMessage('请填写完整的登录信息', 'error');
                return;
            }

            const loginBtn = document.querySelector('.login-btn');
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';

            try {
                let loginUrl = currentUserType === 'admin' ? '/admins/login' : '/users/login';
                
                const response = await fetch(loginUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                if (response.ok) {
                    const result = await response.json();
                    
                    if (result.success) {
                        // 保存用户信息到localStorage
                        const userData = {
                            userId: result.user.userId,
                            username: result.user.username,
                            isAdmin: result.user.admin || result.userType === 'admin'
                        };
                        localStorage.setItem('currentUser', JSON.stringify(userData));
                        
                        showMessage('登录成功！', 'success');
                        
                        // 根据用户类型跳转
                        setTimeout(() => {
                            if (userData.isAdmin) {
                                window.location.href = 'admin.html';
                            } else {
                                window.location.href = 'index.html';
                            }
                        }, 1000);
                    } else {
                        showMessage(result.message || '登录失败', 'error');
                    }
                } else {
                    showMessage('网络错误，请稍后重试', 'error');
                }
            } catch (error) {
                console.error('登录错误:', error);
                showMessage('网络错误，请稍后重试', 'error');
            } finally {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            }
        }

        // 显示消息
        function showMessage(text, type = 'error') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';

            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 5000);
        }
    </script>
</body>

</html>